package com.sinven.service;
 
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sinven.mapper.ArticleMapper;
import com.sinven.pojo.Article;
import com.sinven.pojo.Columntype;
import com.sinven.util.PageBean;
 
@Service
public class ArticleServiceImpl implements ArticleService{
	     //这个注解就是spring可以自动帮你把bean里面引用的对象的setter/getter方法省略
		@Autowired
		private ArticleMapper articleMapper;
		//重写
		@Override
		/**
		 * 增加文章的方法
		 */
		public boolean AddArticle(Article article) throws Exception {
			
			int count = articleMapper.AddArticle(article);
			
			if(count>0) {
				return true;
			}
			return false;
		}
		//重写
		@Override
		/**
		 * 用户的删除
		 */
		public boolean DeletedSelfArticle(int a_id) throws Exception {
			int count = articleMapper.DeletedSelfArticle(a_id);
			if(count>0) {
				return true;
			}
			return false;
		}
		//重写
		@Override
		/**
		 * 管理员删除
		 */
		public boolean DeletedLockArticle(int a_id) throws Exception {
			int count =articleMapper.DeletedLockArticle(a_id);
			if(count>0) {
				return true;
			}
			return false;
		}
		//重写
		@Override
		/**
		 * 完全删除一个文章
		 */
		public boolean Delete(int a_id) throws Exception {
			int count=articleMapper.Delete(a_id);
			if(count>0) {
				return true;
			}
			return false;
		}
		//重写
		@Override
		/**
		 * 清空个人回收站(
		 */
		public boolean DeletedUserRecycle(int a_authorId) throws Exception {
			int count=articleMapper.DeletedUserRecycle(a_authorId);
			if(count>0) {
				return true;
			}
			return false;
		}
		//重写
		@Override
		/**
		 * //清空异常（管理员删除的）
		 */
		public boolean DeletedLockRecycle(int a_authorId) throws Exception {
			int count=articleMapper.DeletedLockRecycle(a_authorId);
			if(count>0) {
				return true;
			}
			
			return false;
		}

		@Override
		/**
		 * 修改
		 */
		public boolean UpdataArticle(Article article) throws Exception {
			int count=articleMapper.UpdataArticle(article);
			if(count>0) {
				return true;
			}
			return false;
		}

		@Override
		/**
		 * 查询文章内容
		 */
		public Article SelectArticleContent(long a_id) throws Exception {
			Article article=articleMapper.SelectArticleContent(a_id);
			if(article==null) {
				System.out.println("失败");
				return null;
			}
			
			return article;
		}

		@Override
		/**
		 * 查询一个人的文章总数
		 */
		public int SelectlUserArticleCount(int a_columnId) throws Exception {
			int count=articleMapper.SelectlUserArticleCount(a_columnId);
			return count;
		}
		@Override
		/**
		 * 查询文章  按照时间先后查询
		 */
		public List<Article> SelectArticleBypostTime() throws Exception {
			List<Article> article = articleMapper.SelectArticleBypostTime();
			if(article==null) {
				System.out.println("查询失败");
				return null;
			}
			return article;
		}
		/**
		 * 查询文章类型
		 */
		@Override
		public List<Columntype> SelectColumntype() throws Exception {
			List<Columntype> columntype = articleMapper.SelectColumntype();
			if(columntype==null) {
				System.out.println("查询失败");
				return null;
			}
			return columntype;
		}
		/**
		 * 查询文章  按照时间先后与文章类型查询
		 */
		@Override
		public List<Article> SelectArticleBypostTimeAndType(int a_columnId) throws Exception {
			List<Article> article = articleMapper.SelectArticleBypostTimeAndType(a_columnId);
			if(article==null) {
				System.out.println("查询失败");
				return null;
			}
			return article;
		}
		/**
		 * 查询文章  按照搜索框查询
		 */
		@Override
		public List<Article> SelectArticleByArticleSearchText(String articleSearchText) throws Exception {
			List<Article> article = articleMapper.SelectArticleByArticleSearchText(articleSearchText);
			if(article==null) {
				System.out.println("查询失败");
				return null;
			}
			return article;
		}
		
		@Override
		public PageBean<Article> SelectArticleBypostTimeAndstate(int state,int pageNum,int pageSize) {
			List<Article> list = articleMapper.SelectArticleBypostTimeAndstate(state);
//			Article article=new Article();
			int totalRecord=list.size();
			System.out.println(totalRecord);
			PageBean<Article> pb=new PageBean<Article>(pageNum, pageSize, totalRecord);
			int startIndex=pb.getStartIndex();
			pb.setList(articleMapper.SelectArticleBypostTimeAndstateByFenYe(state, startIndex, pageSize));
			System.out.println(pb.getList().size());
			
			return pb;
		}
		@Override
		public PageBean<Article> SelectArticleById(int a_authorId,int pageNum,int pageSize) throws Exception{
			//1\得到数据
			
			List<Article> list=articleMapper.SelectArticleById(a_authorId);
			
			Article article=new Article();
			//在这里就要将PageBean中的数据创建好，然后将该对象传回去
			//先要从从数据库中获取所有用户的数据量有多少，获得totalRecord
			int totalRecord=list.size();
			System.out.println(totalRecord);
			//创建PageBean对象 有三个初始数据
			PageBean<Article> pb=new PageBean<Article>(pageNum, pageSize, totalRecord);
			//获取对象PageBean对象中startIndex
			int startIndex=pb.getStartIndex();
			//有了startIndex和pageSize,就可以得到每页的数据了
			//调用Mapper得到当前页数据  #startIndex   # pagesize
			pb.setList(articleMapper.SelectFenYe(a_authorId, startIndex, pageSize));
			//pb.setList();
			return pb;
		}
		@Override
		public boolean AddPageview(int a_id) throws Exception {
			int count = articleMapper.AddPageview(a_id);
			if(count>0) {
				return true;
			}
			return false;
		}
		/**
		 * 查询显示个人删除文章
		 */
		@Override
		public List<Article> SelectPersonalDeleted(int a_authorId) throws Exception {
			List<Article> list=articleMapper.SelectPersonalDeleted( a_authorId);
			if(list==null) {
				System.out.println("查询失败");
				return null;
			}
			return list;
		}
		/**
		 * 查询显示未发布文章
		 */
		@Override
		public List<Article> SelectNotRelease(int a_authorId) throws Exception {
			List<Article> list=articleMapper.SelectNotRelease( a_authorId);
			if(list==null) {
				System.out.println("查询失败");
				return null;
			}
			return list;
		}
		@Override
		public boolean ReductionArticle(int a_id) throws Exception {
			int count =articleMapper.ReductionArticle(a_id);
			if(count>0) {
				
				return true;
			}
			return false;
		}
		@Override
		public List<Article> SelectArticleById(Integer u_id) throws Exception {
			// TODO Auto-generated method stub
			return null;
		}


	
		/**
		 * 修改文章状态
		 */
		@Override
		public boolean UpdateState(int a_id, int state) {
			int count = articleMapper.UpdateState(a_id, state);
			if(count>0) {
				return true;
			}
			return false;
		}
		

		
}
